package com.hyh.eblog.controller;


import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyh.eblog.common.lang.Result;
import com.hyh.eblog.entity.UserMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Summerday
 * @since 2020-10-19
 */
@Controller
public class UserMessageController extends BaseController {


    @GetMapping("/user/message")
    public String message() {

        Page page = userMessageService.paging(getPage(), new QueryWrapper<UserMessage>()
                .eq("to_user_id", getProfileId())
                .orderByAsc("created"));

        request.setAttribute("pageData", page);
        return "/user/message";
    }

    @ResponseBody
    @PostMapping("/message/remove")
    public Result removeAll(Long id, @RequestParam(defaultValue = "false") Boolean all) {


        boolean remove = userMessageService.remove(new QueryWrapper<UserMessage>()
                .eq("to_user_id", getProfileId())
                .eq(!all, "id", id));

        return remove ? Result.success("删除成功") : Result.fail("删除失败");

    }


    @ResponseBody
    @PostMapping("/message/nums")
    public Map<String,Integer> msgNums() {
        int count = userMessageService.count(new QueryWrapper<UserMessage>().
                eq("to_user_id", getProfileId())
                .eq("status", "0"));
        return MapUtil.builder("status", 0).put("count", count).build();
    }


}
